
# coding: utf-8
#

from multiprocessing import managers
from os import name
from schedule import Scheduler
import uiautomator2 as u2
import schedule
import threading
from logzero import logger
import time
from datetime import datetime
import random
from tornado.ioloop import IOLoop
import uiautomator2
from remote.wechat.operators import AboutMy, App, Contact, GroupRoom, PageName
from .jobs import Job, jobManager
from .enums import JobType

def add_friends_job(devic_id: str):
    now = datetime.now()
    logger.info("device %s is running", devic_id)
    app = App(u2.connect(devic_id))

    group_room = GroupRoom(app)
    groups = []
    contact = Contact(app)
    
    app.start(True)
    
    app.wait_homepage()
    
    if(app.is_homepage()):
        group_room.navigate_contacts()
        time.sleep(0.5)
    if(app.is_contactbook()):
        group_room.navigate_groups()
        time.sleep(0.5)
    if(app.is_contact_chatroom()):
        groups = group_room.collect_groups(True)
        group_room.navigate_chating(random.randint(0, len(groups) - 1))
        time.sleep(0.5)
    if(app.is_chating()):
        contact.navigate_chat_info(True)
        time.sleep(0.5)
    if(app.is_chatroom_info() or app.is_room_member_info()):
        contact.navigate_member_info(3)
        time.sleep(0.5)
    if(app.is_contact_info()):
        contact.send_friend_invitation()
    end = datetime.now()

    logger.info("The process cost the %s", (end - now).seconds)
def test_setup_permission():
    app = App(u2.connect("1591998a0806"))
    app.start(True)
    app.wait_homepage()
    me = AboutMy(app)
    me.disabled_friend_permission()

def run_threaded(job_func, name: str):
    job_thread = threading.Thread(target=add_friends_job, args=(name, ))
    job_thread.start()

def test_job_manager():

    plan_datetime1 = datetime(2020, 11, 10, 2, 28)
    operator_data = { 
        'current_phone_index': 0, 
        'cellphones': [
            '18627711192',
            '18627711193',
            '18502752425'
        ]}
    job = Job("1591998a0806-wechat-addfriendsinexcel-0", device_serial="1591998a0806", \
            app_name="wechat", job_type=JobType.AddFriendFromExcel.job_type, \
            task_load=3, plan_datetime = plan_datetime1, operator_data=operator_data, task_generating_pattern=0.2)
    jobManager.try_accept(job)

def test_start():
    """
    程序初始化测试
    1. 确保程序重新启动
    2. 确保首页能呈现出来。
    """
    app = App(u2.connect("15793cac0806"))
    app.start(True, True, False)

def test_ensure():
    app = App(u2.connect("15793cac0806"))
    app.ensure_homepage()

def main():
    print("start")
    #test_setup_permission()
    test_job_manager()
    # logger.info("device %s is running", "1591998a0806")
    # add_friends_job("1591998a0806")

   
if __name__ == "__main__":
    # app = App(u2.connect("1591998a0806"))
    # while True:
    #     if app.is_app_opened() :
    #         logger.info("微信打开")
    #     else:
    #         logger.warning("微信没有打开")
    #     time.sleep(1)

    main()
    try:
        IOLoop.current().start()
    except KeyboardInterrupt:
        logger.info("Interrupt catched")
    
   